package com.todo.event.activity.application.core.engine.aviator.function;


import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Map;

/**
 * @author azhebuxing
 * @date 2025/1/9 08:25
 * @description 判断一个字符串是否在一个字符串区间集合中
 */
public class InList extends AbstractFunction {

    public static final String EXP = "inList(%s,%s,%s)";

    /**
     * @param env  环境参数
     * @param arg1 校验数据
     * @param arg2 分隔符
     * @param arg3 集合数据
     * @return
     */
    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
        Object target = arg1.getValue(env);
        //对于数字类型，保留整数部分
        String formatValue = String.valueOf(target);
        Object split = arg2.getValue(env);
        Object collection = arg3.getValue(env);
        boolean in = Arrays.asList(collection.toString().split(split.toString())).contains(formatValue);
        return AviatorBoolean.valueOf(in);
    }

    @Override
    public String getName() {
        return "inList";
    }

    public static String buildInListExp(String fieldName, String value, String split) {
        return String.format(EXP, fieldName, "'" + split + "'", value);
    }
}
